package com.kuang.springCloud.controller;

import com.kuang.springCloud.pojo.Dept;
import com.kuang.springCloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//提供restful服务:
//@RestController = @Controller + @RespondBody
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;
    @Autowired
    private DiscoveryClient client; //获取一些配置信息，得到具体的微服务

    @PostMapping("dept/add/")
    public boolean add(Dept dept){
        return deptService.addDept(dept);
    }
    @GetMapping("dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return deptService.queryById(id);
    }
    @GetMapping("dept/list")
    public List<Dept> list(){
        return deptService.queryAll();
    }

    //注册进来的服务，获取一些信息   //团队开发会使用到
    @GetMapping("/dept/discovery")
    public Object discovery(){
        //获得服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery=>services:"+services);
        //得到具体的微服务信息，通过服务的applicationName来取值
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT-8001");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost()+"/t"+
                                instance.getPort()+"/t"+
                                instance.getUri()+"/t"+
                                instance.getServiceId());
        }

        return this.client;
    }
}
